package ch.abcm.remsmobile.REMSMobile.externnotifier;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;
import ch.abcm.remsmobile.REMSMobile.externnotifier.utils.NotificationLogger;
import ch.abcm.remsmobile.REMSMobile.history.MessageHistoryActivity;
import ch.abcm.remsmobile.REMSMobile.preferences.PrefsActivity;

public class ExternNotifierController extends BroadcastReceiver {

	private static final String TAG = "ExternNotifierController";
	private static final String EXTERN_NOTIFIER_ACTION = "ch.bfh.abcm.rems.EXTERN_NOTIFIER_ACTION";
	private static final String EXTERN_NOTIFIER_TEST_ACTION = "ch.bfh.abcm.rems.EXTERN_NOTIFIER_TEST_ACTION";
	private static final String MESSAGE_EXTRA_TYPE = "MessageType";
	private static final String MESSAGE_EXTRA_TEXT = "MessageText";

	private enum NotifType {
		LOG, TOAST, SMS;
	}

	public enum MessageType {
		PERIODICUPDATE, SYSTEAM_ERROR, MEASUREMNET_DIVERGENCE, SYSTEM_OK, LOCAL;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		String message = intent.getStringExtra(MESSAGE_EXTRA_TEXT);
		Log.i(TAG, "Received Action: " + action);

		if (action != null && action.equalsIgnoreCase(EXTERN_NOTIFIER_TEST_ACTION)) {
			Log.i(TAG, "Send a Test SMS");
			sendSMS(context, message);
		} else if (action != null && action.equalsIgnoreCase(EXTERN_NOTIFIER_ACTION)) {
			Log.i(TAG, "Notify the Recepient...");
			NotifType notifType = getNotificationType(context);
			Log.d(TAG, "NotifType: " + notifType);
			Log.d(TAG, "NotificationMessage:"+message);
			// for HistoryList
			Intent historyIntent = new Intent(MessageHistoryActivity.INTENT_ACTION);
			historyIntent.putExtra("message", message);
			context.sendBroadcast(historyIntent);
			
			boolean isPN = false;
			boolean isOK = false;
			if (intent.getStringExtra(MESSAGE_EXTRA_TYPE)!=null) {
				isPN = (intent.getStringExtra(MESSAGE_EXTRA_TYPE).equals(MessageType.PERIODICUPDATE+""));
				isOK = (intent.getStringExtra(MESSAGE_EXTRA_TYPE).equals(MessageType.SYSTEM_OK+""));
			}
			new NotificationLogger().appendMessageToFile(context, message, isPN, isOK);
			
			switch (notifType) {
			case SMS:
				String gsmMessage = message.replace("°", "");
				sendSMS(context, gsmMessage);
				break;
			case TOAST:
				Log.i(TAG, message);
				Toast.makeText(context, message, Toast.LENGTH_LONG).show();
				break;
			case LOG:
				Log.i("NOTIF", message);
				break;
			}

		}
	}

	private NotifType getNotificationType(Context context) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		String notifType = prefs.getString(PrefsActivity.KEY_PREFRENCES_NOTIFICATION_TYPE, "0");
		Log.d(TAG, "String notiftype:"+notifType);
		// This is not a god solution, but its late and time ir running.. 
		if (notifType!=null) {
			if (notifType.equalsIgnoreCase("LOG")) {
				return NotifType.LOG;
			}
			if (notifType.equalsIgnoreCase("TOAST")) {
				return NotifType.TOAST;
			}
			if (notifType.equalsIgnoreCase("SMS")) {
				return NotifType.SMS;
			}
		}
		// Ok send a SMS
		return NotifType.SMS;
	}

	public static Intent getSimpleMessageIntent(String message) {
		Intent simpleMessageIntent = new Intent(EXTERN_NOTIFIER_ACTION);
		simpleMessageIntent.putExtra(MESSAGE_EXTRA_TYPE, MessageType.LOCAL);
		simpleMessageIntent.putExtra(MESSAGE_EXTRA_TEXT, message);
		return simpleMessageIntent;
	}
	
	public static Intent getSimpleMessageIntent(String message, MessageType type) {
		Intent simpleMessageIntent = new Intent(EXTERN_NOTIFIER_ACTION);
		simpleMessageIntent.putExtra(MESSAGE_EXTRA_TYPE, type+"");
		simpleMessageIntent.putExtra(MESSAGE_EXTRA_TEXT, message);
		return simpleMessageIntent;
	}

	private void sendSMS(Context context, String message) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		String number = prefs.getString(PrefsActivity.KEY_PREFRENCES_RECEPIENT, "0797897858");
		Log.d(TAG, "Number: " + number);
		if (number.matches("^(07[6|8|9][0-9]{7})")) {
			Log.d(TAG, "SEND SMS: "+message);
			SmsManager smsManager = SmsManager.getDefault();
			smsManager.sendTextMessage(number, null, message, null, null);
			
		}	else {
			String errorText = "No valid MSISDN";
			Toast.makeText(context, errorText, Toast.LENGTH_LONG).show();
			Log.e(TAG, errorText);
		}
		
		
	}
}
